//
//  ConfirmOrderViewController.m
//  firstpoint
//
//  Created by 泽中 on 2017/7/25.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "ConfirmOrderViewController.h"
#import "GuiGeXiangQingView.h"
#import "BinDingTextView.h"
#import "PayTypeView.h"
#import "DaiFuView.h"
#import "PayPwdView.h"
#import "PromptView.h"
@interface ConfirmOrderViewController ()
{
    UILabel*nameLab;
    UILabel*phoneLab;
    UILabel*shouhuoDiZhi;
    GuiGeXiangQingView*guigeView;
    BinDingTextView*peisongfangshi;
    BinDingTextView*yunfei;
    PayTypeView*yuanbaoPayView;
    PayTypeView*jinbiPayView;
    DaiFuView*daifuView;
    PayPwdView*payPwdView;
    UIView*qianheiView;
    NSDictionary*dataDic;
    NSString*payTypeId;
    UILabel*shifuLab;
    NSArray*dizhiArr;
    NSString*codeStr;
}
@end

@implementation ConfirmOrderViewController
-(void)myZhangHuHttpRquest
{
    NSString *url = [NSString stringWithFormat:@"%@%@",LINK_BASE_URL(@"/user/accountInfo.api?access_token="),NSUD_Obj(@"token")];
    [HTTPURL postRequest:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        dataDic= [PublicTools dictionaryWithJsonString:responseObject[@"data"][@"userDTO"]];
        NSLog(@"%@",dataDic);
        yuanbaoPayView.jiage.text=[NSString stringWithFormat:@"%ld元宝",[dataDic[@"ingot"]integerValue]];
        jinbiPayView.jiage.text=[NSString stringWithFormat:@"%ld金币",[dataDic[@"gold"]integerValue]];
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];
}
-(void)chaxunList//查询
{
    NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"address/addressSelectList.api?access_token=%@",tokenStr];
    [HTTPURL postRequest:LINK_BASE_URL(urlStr) parameters:@{@"userId":userid} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary*dic=[PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        dizhiArr=(NSArray*)dic;
        NSString*isMR;
        for (int i=0; i<dizhiArr.count; i++) {
            isMR=dizhiArr[i][@"isDefault"];
            if ([isMR isEqualToString:@"T"]) {
                nameLab.text=dizhiArr[i][@"name"];
                phoneLab.text=dizhiArr[i][@"phone"];
                shouhuoDiZhi.text=dizhiArr[i][@"address"];
            }

        }
            } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    payTypeId=@"1001";
    self.title=@"确认订单";
    [self chaxunList];
    [self myZhangHuHttpRquest];
    self.view.backgroundColor=LRRGBColor(235, 240, 242);
//    self.view.backgroundColor=[UIColor whiteColor];
    [self setUI];
}
-(void)setUI
{
    UIScrollView*scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];
//    scrollView.backgroundColor=LRRGBColor(235, 240, 242);
    [self.view addSubview:scrollView];
    //收件人
    UIView*dizhiView=[[UIView alloc]initWithFrame:CGRectMake(0, 74, width_screen, 78)];
    dizhiView.backgroundColor=[UIColor whiteColor];
    [scrollView addSubview:dizhiView];
    nameLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 18)];
    nameLab.text=@"王小明";
    nameLab.font=[UIFont fontWithName:@"Arial-BoldMT"size:18];
    
    [dizhiView addSubview:nameLab];
    
    phoneLab=[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 150, 18)];
    phoneLab.text=@"15911028373";
    phoneLab.font=[UIFont fontWithName:@"Arial-BoldMT"size:18];
    
    [dizhiView addSubview:phoneLab];
    shouhuoDiZhi=[[UILabel alloc]initWithFrame:CGRectMake(10, 28, width_screen-20, 50)];
    shouhuoDiZhi.font=[UIFont systemFontOfSize:14];
    shouhuoDiZhi.textColor=[UIColor lightGrayColor];
    shouhuoDiZhi.text=@"收货地址:四大神兽大所多撒多撒多撒四大神兽大所多撒多撒多撒四大神兽大所多撒多撒多撒四大神兽大所多撒多撒多撒四大神兽大所多撒多撒多撒四大神兽大所多撒多撒多撒";
    shouhuoDiZhi.numberOfLines=0;
    [dizhiView addSubview:shouhuoDiZhi];
    
    //规格详情
    
    guigeView=[[GuiGeXiangQingView alloc]initWithFrame:CGRectMake(0, 162, width_screen, 95)];
    guigeView.imgView.backgroundColor=[UIColor redColor];
    guigeView.titleLab.text=_titleStr;
    guigeView.guigeLab.text=@"规格:170";
    guigeView.colorLab.text=@"颜色:白色";
    guigeView.jiageLab.text=[NSString stringWithFormat:@"%@-%@",_yuanbao,_jinbi];
    guigeView.numLab.text=@"x 1";
    [scrollView addSubview:guigeView];
    //配送方式
    peisongfangshi=[[BinDingTextView alloc]initWithFrame:CGRectMake(0, 258, width_screen, 45)];
    peisongfangshi.titleLab.text=@"配送方式";
    peisongfangshi.textField.placeholder=@"第三方快递";
    peisongfangshi.userInteractionEnabled=NO;
//    peisongfangshi.textField.backgroundColor=[UIColor redColor];
    [scrollView addSubview:peisongfangshi];
    //运费
    yunfei=[[BinDingTextView alloc]initWithFrame:CGRectMake(0, 304, width_screen, 45)];
    yunfei.titleLab.text=@"运费";
    yunfei.textField.placeholder=@"保佑";
    yunfei.userInteractionEnabled=NO;
    [scrollView addSubview:yunfei];
    //元宝
    yuanbaoPayView=[[PayTypeView alloc]initWithFrame:CGRectMake(0, 360, width_screen, 45)];
    yuanbaoPayView.title.text=@"元宝支付";
    yuanbaoPayView.button.selected=YES;
    yuanbaoPayView.jiage.text=@"182元宝";
    [yuanbaoPayView.button addTarget:self action:@selector(yuanbaoPayButton) forControlEvents:64];
    [scrollView addSubview:yuanbaoPayView];
    //金币
    jinbiPayView=[[PayTypeView alloc]initWithFrame:CGRectMake(0, 406, width_screen, 45)];
    jinbiPayView.title.text=@"金币支付";
    jinbiPayView.jiage.text=@"182金币";
    [jinbiPayView.button addTarget:self action:@selector(jinbiPayButton) forControlEvents:64];

    [scrollView addSubview:jinbiPayView];
   //找人代付
    daifuView=[[DaiFuView alloc]initWithFrame:CGRectMake(0, 462, width_screen, 45)];
    [scrollView addSubview:daifuView];
    //底部
   shifuLab=[[UILabel alloc]initWithFrame:CGRectMake(0, height_screen-50, width_screen/5*3, 50)];
    shifuLab.backgroundColor=[UIColor whiteColor];
    shifuLab.text=[NSString stringWithFormat:@"实付:%@",_yuanbao];
    shifuLab.textColor=[UIColor redColor];
    shifuLab.textAlignment=NSTextAlignmentCenter;
    [scrollView addSubview:shifuLab];
    
    UIButton*xiadanButton=[UIButton buttonWithType:UIButtonTypeCustom];
    xiadanButton.backgroundColor=LRRGBColor(215, 25, 36);
    [xiadanButton setTitle:@"立即下单" forState:0];
    xiadanButton.frame=CGRectMake(width_screen/5*3, height_screen-50, width_screen/5*2, 50);
    [xiadanButton addTarget:self action:@selector(xiadanButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:xiadanButton];
}
-(void)yuanbaoPayButton
{  shifuLab.text=[NSString stringWithFormat:@"实付:%@",_yuanbao];
    yuanbaoPayView.button.selected=YES;
    jinbiPayView.button.selected=NO;
    payTypeId=@"1001";
}
-(void)jinbiPayButton
{
    shifuLab.text=[NSString stringWithFormat:@"实付:%@",_jinbi];
    yuanbaoPayView.button.selected=NO;
    jinbiPayView.button.selected=YES;
    payTypeId=@"1002";

}
-(void)xiadanButtonClick
{  [self xiadanHttpaRequest];
    qianheiView=[[UIView alloc]initWithFrame:self.view.bounds];
    qianheiView.backgroundColor=[UIColor blackColor];
    qianheiView.alpha=.7;
    [self.view addSubview:qianheiView];
    payPwdView=[[PayPwdView alloc]initWithFrame:CGRectMake(50, 200, width_screen-100, 165)];
    [payPwdView.quxiaoButton addTarget:self action:@selector(quxiaoZhiFu) forControlEvents:UIControlEventTouchUpInside];
    [payPwdView.quedingButton addTarget:self action:@selector(querenZhiFu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payPwdView];

}
-(void)xiadanHttpaRequest
{
    NSString *url = [NSString stringWithFormat:@"%@%@",LINK_BASE_URL(@"/selfOrder/save.api?access_token="),NSUD_Obj(@"token")];

    [HTTPURL postRequest:url parameters:@{@"goodsId":_shangpinId,@"userId":userid,@"deliveryAddressId":@"1",@"buyCount":@"1",@"payTypeId":payTypeId,@"props":@"props"} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        codeStr=responseObject[@"data"];
        [PromptView showView:responseObject[@"retInfo"] style:1];

   } filure:^(NSURLSessionDataTask *task, id error) {
       
   }];
   
}
-(void)quxiaoZhiFu
{
    
    [qianheiView removeFromSuperview];
    [payPwdView removeFromSuperview];
}
-(void)querenZhiFu
{
    NSString *url = [NSString stringWithFormat:@"%@%@",LINK_BASE_URL(@"transaction/pay.api?access_token="),NSUD_Obj(@"token")];
    
    [HTTPURL postRequest:url parameters:@{@"userId":userid,@"payPassword":payPwdView.textField.text,@"orderCodes":codeStr} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        [PromptView showView:responseObject[@"retInfo"] style:1];

    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];

    [qianheiView removeFromSuperview];
    [payPwdView removeFromSuperview];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
